<?

/***********************************************************/
/* Translates the message
/***********************************************************/
function _a($msg, $param1 = null, $param2 = null, $param3 = null)
{
    return Trans::Translate($msg, $param1, $param2, $param3);
}

/**
 * For a selected file and a selected action, popup a box asking to confirm
 * the action.
 *
 * @param	array	Array of callback function, message, mask and default path
 * @param   type    string of gtk constante referente a salvar ou abrir arquivo
 */
function HandlerFile($extraFunction = null,$type=null )
{  //array('1',  array(&$this, 'Abrir'), _a('Open'), null, $this->GetMagicPath())
	$Verify = $extraFunction[0];
	$Array = $extraFunction[1];//array(&$this, 'Abrir')
    $Function =$Array[1];
	$message = $extraFunction[2];
	$Mask = $extraFunction[3];
	$DefaultPath = $extraFunction[4];
    //echo "$Function";
    //print_r($Function);
	eval("\$a = $Verify;");
	if ($a)
    {   //$save deve ser Gtk::FILE_CHOOSER_ACTION_SAVE OR Gtk::FILE_CHOOSER_ACTION_OPEN
                       //$title, $filter = null, $extrabutton = null, $firstdir = null, $Action = null, $Parameter = null
       if($type=='')
       {
       $isdir=Gtk::FILE_CHOOSER_ACTION_OPEN;
       
       }else{
            $isdir=Gtk::FILE_CHOOSER_ACTION_SAVE;
            
            }
       
       //$save=Gtk::FILE_CHOOSER_ACTION_SAVE;
       $isdir=
       $FS = &new FileDialog($message,$isdir, null, null, $DefaultPath,$Function,null,$save);         
       echo "\n util 41 $FS->resultado ..";

		if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
		{
			$bar = '\\';
		}
		else
		{
			$bar = '/';
		}

		if($FS->resultado[2]){//
        $selected_dir=$FS->resultado[1];
        $selected_file=$FS->resultado[0];
        echo "\nutil.inc 53 resultado[1] $FS->resultado[1] = $selected_dir \nutil.inc 53 resultado[0] $FS->resultado[0] = $selected_file";
        //$chama_funcao=$Function.'($selected_file);';//'$this->'.$Function.'($selected_file);';
        //echo "$chama_funcao";
        //eval($chama_funcao);
        return $selected_file;
		//$FS->connect_simple('response', $Array, $FileSelection);
        //$this->Abrir();
        }else{
             return null; //foi apertado o botao cancelar em FileDialog.class
             }
	} 
	else //!$DefaultPath
	{
		new Dialog($message);
	}
}
  
  /**
   * Returns a simple array from a directory
   *
   * @param	string	Path of the directory
   * @param	bool	Wether or not to list only directories (or also other files)
   * @param	string	Filter to apply to the filename before putting it into the array
   * @return	array	Array of directories (and files) names
   * @throws	null	Should throw exceptions on system calls
   */
function GetSimpleDirArray($sPath, $onlydir, $filter)
{
	$handle=opendir($sPath);
	while ($file = readdir($handle))
	{
		$nPath = "$sPath/$file";
		$is_dir = is_dir($nPath);
                                                                                                                 
		if (!$onlydir)
		{
			$is_dir = !$is_dir;
		}
                                                                                                                 
		if ($is_dir && ($file != '.') && ($file != '..'))
		{
			if ($filter)
			{
				if (strstr($file, $filter))
				{
					$dirs[' ' . $file] = $file;
				}
			}
			else
			{
				$dirs[' ' . $file] = $file;
			}
		}
	}
	closedir($handle);
	if ($dirs)
	{
		ksort($dirs);
	}
	return $dirs;
}


/*****************************************************************************
 ** Returns the file name from a complete path
 *****************************************************************************/
function GetFileName($Path)
{
    $tmp = strrev($Path);
    $result = $tmp;

    for ($n=0; $n<strlen($tmp); $n++)
    {
        if ((substr($tmp,$n,1) == '/') || (substr($tmp,$n,1) == '\\'))
        {
            $result = substr($tmp,0,$n);
            break;
        }
    }
    $result = strrev($result);
    return $result;
}

/******************************************************************************
 ** Returns the file name from a complete path
 *****************************************************************************/
function GetPath($Path)
{
	$tmp = strrev($Path);

	for ($n=0; $n<strlen($tmp); $n++)
	{
		if ((substr($tmp,$n,1) == '/') || (substr($tmp,$n,1) == '\\'))
		{
			$result = substr($tmp,$n);
			break;
		}
	}
	$result = strrev($result);
	return $result;
}

/******************************************************************************
 ** Gets the parent directory
 *****************************************************************************/
function get_parent_dir($path)
{
    if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
    {
        $split = explode('\\', $path);
    }
    else
    {
        $split = explode('/', $path);
    }
    //var_dump($split);

    for ($n=0; $n<count($split) -1; $n++)
    {
        $return .= $split[$n] . bar;
    }

    return $return;
}

/******************************************************************************
 ** Converts the path format.
 *****************************************************************************/
function win2lin($path)
{
    $split = explode(':', $path);
    if (count($split) >1)
    {
        $path2 = $split[1];
    }
    else
    {
        $path2 = $path;
    }
    $path2 = str_replace('\\' ,'/', $path2);
    return $path2;
}

/******************************************************************************
 ** Removes the tags from a string
 *****************************************************************************/
function RemoveTags($string)
{
    $fl_inside = false;
    for ($n=0; $n<=strlen($string); $n++)
    {
        $char = substr($string,$n,1);
        if ($char == '<')
        {
            $fl_inside = true;
        }

        if (!$fl_inside)
        {
            $return .= $char;
        }
        if ($char == '>')
        {
            $fl_inside = false;
        }

    }
    return $return;
}

function rgb2int($rgb)
{
    $hex_red   = substr($rgb,1,2);
    $hex_green = substr($rgb,3,2);
    $hex_blue  = substr($rgb,5,2);
    
    $dec_red = hexdec($hex_red);
    $dec_green = hexdec($hex_green);
    $dec_blue = hexdec($hex_blue);

    $int_red = $dec_red/255;
    $int_green = $dec_green/255;
    $int_blue = $dec_blue/255;

    return array($int_red, $int_green, $int_blue);
}


function invcolor($rgb)
{
    $int = rgb2int($rgb);
    $c_red   = (1- $int[0]) * 255;
    $c_green = (1- $int[1]) * 255;
    $c_blue  = (1- $int[2]) * 255;

    $red   = dechex($c_red);
    $green = dechex($c_green);
    $blue  = dechex($c_blue);
    
    $red   = substr('00', 0, 2-strlen($red)) . $red;
    $green = substr('00', 0, 2-strlen($green)) . $green;
    $blue  = substr('00', 0, 2-strlen($blue)) . $blue;

    return "#$red$green$blue";
}

function central($widget)
{
    $box1 = new GtkHBox;
    $box2 = new GtkHBox;
    $box = new GtkHBox;
    $box->pack_start($box1, true, true);
    $box->pack_start($widget, false, false);
    $box->pack_start($box2, true, true);

    return $box;
}

function left($widget)
{
    $box2 = new GtkHBox;
    $box = new GtkHBox;
    $box->pack_start($widget, false, false);
    $box->pack_start($box2, true, true);

    return $box;
}

function right($widget)
{
    $box2 = new GtkHBox;
    $box = new GtkHBox;
    $box->pack_start($box2, true, true);
    $box->pack_start($widget, false, false);

    return $box;
}

function FirstUpper($string)
{
    return strtoupper(substr($string,0,1)) . strtolower(substr($string,1));
}

function darktxt($text)
{
    $topbox = new gtkeventbox;
    $topbox->set_size_request(-1, 26);
    $style = $topbox->style;
    $style->bg[GTK::STATE_NORMAL] = new GdkColor(42036, 42036, 42036);
    $style = $style->copy();
    $topbox->set_style($style);

    $topbox->add(left(new GtkLabel('  ' . $text)));
    return $topbox;
}

function Cut($msg, $string)
{
    $pos = strpos($msg, $string);
    $error = substr($msg, $pos);
    return $error;
}

?>
